//
//  ProductsViewController.m
//  彩票项目
//
//  Created by 伍银荣 on 2021/8/10.
//  Copyright © 2021 com.WYR. All rights reserved.
//

#import "ProductsViewController.h"
#import "ProductsModel.h"
#import "ProductsCell.h"

static NSString * const reuseIdentifier = @"product_cell";

@interface ProductsViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *products;

@end

@implementation ProductsViewController

-(NSArray *)products {
    if (!_products) {
        NSArray *tempArr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"product.plist" ofType:nil]];
        NSMutableArray *muArr = [NSMutableArray array];
        for (NSDictionary *dict in tempArr) {
            ProductsModel *model = [ProductsModel productWithDict:dict];
            [muArr addObject:model];
        }
        _products = muArr;
    }
    return _products;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"---products = %@",self.products);
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(80, 80);
    //item左右最小的距离
    layout.minimumInteritemSpacing = 10;
   // layout.minimumLineSpacing = 20;
    //组内边距
    layout.sectionInset = UIEdgeInsetsMake(16, 0, 0, 0);
    _collectionView = [[UICollectionView alloc]initWithFrame:kScreenSize collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    //注册单元格
    [_collectionView registerNib:[UINib nibWithNibName:@"ProductsCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
   
}

//返回一组
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.products.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    //从缓存池中找cell
    ProductsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
        cell.model = self.products[indexPath.row];
    return cell;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
